package com.financing.wap.weixin.handle.event;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.dto.DstUserInfoDTO;
import com.financing.wap.service.IDstUserInfoService;
import com.financing.wap.weixin.handle.EventHandler;
import com.financing.wap.weixin.resp.TextMessage;
import com.financing.wap.weixin.util.MessageUtil;

/**
 * 取消关注
 * @author lisu
 *
 */
@Service("unsubscribeEvent")
public class UnsubscribeEvent implements EventHandler{
	
	@Autowired
	private IDstUserInfoService dstUserInfoService;

	@Override
	public String messageHandler(Map<String, String> requestMap) throws Exception{
        // 发送方帐号（open_id）  
        String fromUserName = requestMap.get("FromUserName");  
        // 公众帐号  
        String toUserName = requestMap.get("ToUserName"); 
        
        //取消关注 修改用户关注状态
		DstUserInfoDTO wxUserInfo = new DstUserInfoDTO();
		wxUserInfo.setUserWechatOpenid(fromUserName);
		DstUserInfoDTO dto = dstUserInfoService
				.queryListGetFirst(wxUserInfo);
		if(dto.getId()!=null){
			dto.setFlagConcern(CommonConstantUtil.UserInfoStatus.FLAG_CONCERN_NO);// 取消关注
			dto.setUpdateDate(new Date(System.currentTimeMillis()));
			dstUserInfoService.update(dto);
		}
        
		TextMessage textMessage = new TextMessage();  
        textMessage.setToUserName(fromUserName);  
        textMessage.setFromUserName(toUserName);  
        textMessage.setCreateTime(new Date().getTime());  
        textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);  
        textMessage.setFuncFlag(0);  
        // 由于href属性值必须用双引号引起，这与字符串本身的双引号冲突，所以要转义  
        textMessage.setContent("已经取消关注");  
        // 将文本消息对象转换成xml字符串  
        return MessageUtil.textMessageToXml(textMessage);
         
	}

	@Override
	public void messageSubsequentHandler(Map<String, String> requestMap) {
	}

}
